<?php

/**
 * 分页链接
 * @param $url
 * @param $page
 * @return bool
 */
function get_page($url,$page){

    $cSnoopy = new Snoopy();

    $url        = replace_url($url);	//替换分页特征
    $page       = ((int)$page<=3)?$page:1;
    if((int)$page>3) return false;	//超出3页退出

    $url        = str_ireplace("{page}",$page,$url);

    $cSnoopy->fetch($url);
    $response   = $cSnoopy->results;

    preg_match_all("/<li\s+index='([0-9]{0,2})'.*?sku='([0-9]{0,13})'\s?>/is",$response,$match);

    foreach($match[2] as $key=>$value){
        $lists[$match[1][$key]+1]=$value;
    }

    return $lists;
}

/**
 * 获取分页链接
 * @param $url
 * @param int $pagenum
 * @return string
 */
function replace_url($url,$pagenum=12){
    $url_array=explode('-',$url);
    $url_array[$pagenum]='{page}';
    return implode('-',$url_array);
}

/**
 * 替换空格
 */
function sleep_rand(){
    sleep(rand(2,5));
}

/**
 * 替换空格
 * @param $str
 * @return mixed
 */
function rm_space($str){
    return preg_replace("/(\s|\&nbsp\;|　|\xc2\xa0)/","",$str);
}

/**
 * 获取单个商品的排名
 * @param $item
 * @return int
 */
function get_item_top($item){

    $item_id    = (int)$item['item_id'];
    $url        = $item['url'];
    $page       = $item['page'];
    $top        = 0;

    for($i = 1;   $i<=$page;  $i++){
        //最多抓取三页
        $result = get_page($url,$i);
        sleep_rand();

        if(in_array($item_id,$result)){
            $top= array_search($item_id,$result)+($i-1)*36;
            break;
        }
    }

    //排名
    return (int)$top;

    return $top;
}


/**
 * 按日期添加
 * @param $id
 * @param $rank
 * @param null $date
 */
function add_by_created($id,$rank,$date = null){

    global $db;

    if(is_null($date)){
        $date = date('Y-m-d');
    }

    $sql = "SELECT * FROM jd_rank_log WHERE DATE_FORMAT(created,'%Y-%m-%d') = '$date' AND rank_id = $id ";
    $exists_log = $db->get_one($sql);

    $insert_id = 0;
    //不存在则添加
    if(empty($exists_log)){
        $created = full_date();

        $addSql = <<<EOD
    INSERT INTO jd_rank_log (rank_id,rank,created) VALUES ($id,$rank,'$created')
EOD;

        $db->query($addSql);
        $insert_id = $db->insert_id();
    }else{
        $created = full_date();

        $id = $exists_log['id'];

        $insert_id = $id;

        $addSql = <<<EOD
    UPDATE jd_rank_log SET rank = $rank,created = '$created' WHERE id = $id
EOD;

        $db->query($addSql);
        $insert_id = $db->insert_id();
    }

    return $insert_id;
}


/**
 * 随机获取代理ip
 * @return mixed
 */
function get_rand_proxy_ip()
{
    $path = ROOT_PATH."Cli/proxy.conf";

    $lines = file($path);

    $ip = $lines[rand(0,count($lines)-1)];

    return $ip;
}


















//////////////////// 收货确认 /////////////////////////////

/**
 * 登陆京东用户，获取sid
 * @param $id
 * @param $username
 * @param $passwd
 * @return string
 */
function get_jd_sid($order_id,$member_name,$member_pwd){
    global $cCurl;

    $loginUrl ="http://m.jd.com/user/doLogin.action?v=w";
    $fields ="returnurl=http%3A%2F%2Fm.jd.com%2Fuser%2Fmyorders.action%3Fsid%3Dnull&username={$member_name}&password={$member_pwd}&btnLogin=%E7%99%BB%E5%BD%95&v=t";
    $response = $cCurl->post($loginUrl,$fields,$referurl="http://m.jd.com/user/login.action");

    $curl_info = $response['curl_info'];

    //状态码
    $http_code = $curl_info['http_code'];
    //内容
    $content   = $response['content'];

    cli_out('http_code : '.$http_code);

    if($http_code == '403'){
        cli_out('403 Forbidden');
    }else if($http_code == '200'){

    }

    //请求失败，要换ip
    if($response === false || is_null($http_code)){
        return false;
    }

    cli_out('content:'.$content);
    cli_out('sid url : '.$curl_info['url']);

    //要求重新登录
    if($curl_info['url'] == 'http://m.jd.com/user/doLogin.action?v=w'){
        return false;
    }

    if(stristr($curl_info['url'],'sid')){
        list(,$sid)=explode('sid=',$curl_info['url']);
    }else{
        $sid= '';
    }

    //记录sid内容
    //$up_items = array('sid_content' => $content);
    //order_update($order_id,$up_items);

    return $sid;
}


/**
 * 获取订单列表
 * @param $order_id
 * @param $sid
 * @param $member_name
 * @param $member_pwd
 * @return mixed
 */
function get_order_status($order_id,$order_sn,$sid){

    global $cCurl;

    $referurl   = "http://passport.m.jd.com/user/home.action?sid=$sid";
    $get_url    = "http://passport.m.jd.com/user/myorders.action?sid=$sid";


    cli_out('get order status from :'.$get_url);

    $response = $cCurl->get($get_url,$referurl);

    $content = $response['content'];

    cli_out('orders_content :'.$content);

    //记录sid内容
    //$up_items = array('orders_content'=> $content);
    //order_update($order_id,$up_items);

    //待收货
    $receiveing_reg = "订单号：$order_sn 等待收货";
    //已完成
    $received_reg   = "订单号：$order_sn 完成";

    preg_match('/'.$receiveing_reg.'|'.$received_reg.'/is',$content,$matchs);

    //匹配结果
    $result = (isset($matchs[0])) ? $matchs[0] : '';

    if($result == $receiveing_reg){
        //待收货
        return RECEIVE_RECEIVEING;
    }else if ($result == $received_reg){
        //已完成
        return RECEIVE_RECEIVED;
    }else{
        //未发货
        return RECEIVE_UN_RECEIVE;
    }
}




/**
 * 确认收货一个订单
 * @param $order
 */
function receive_jd_order($order){

    $order_id       = $order['order_id'];
    $order_sn       = $order['order_sn'];
    $member_name    = $order['member_name'];
    $member_pwd     = $order['member_pwd'];


    cli_out('start get sid ,please wait...');

    $get_sid_error_times = 0;
    do{

        //最多试5次
        if($get_sid_error_times > 5){
            break;
        }

        //获取sid
        $sid = get_jd_sid($order_id,$member_name,$member_pwd);

        if($sid === false || $sid == ''){
            $get_sid_error_times++;
            //代理ip不可用
            $change_success = change_proxy_ip();
            if(!$change_success){
                $get_sid_error_times++;
            }
        }

    }while(empty($sid));


    $result = '';
    if($sid){

        cli_out('find sid :'.$sid);
        //检查订单状态是否是待收货

        //收货状态
        $receive_status = get_order_status($order_id,$order_sn,$sid);

        if($receive_status == RECEIVE_RECEIVED){
            cli_out("order :$order_sn already received");
            //已完成
            return true;
        }else if($receive_status == RECEIVE_RECEIVEING){
            //待收货
            cli_out("order :$order_sn receiveing");
        }

        //收货确认
        cli_out("order :$order_sn start receiveing");

        $result = get_jd_confirm($order_id,$order_sn,$sid);
    }else{
        cli_out('not find sid');
    }


    //状态
    $op_success = '操作成功';
    $op_fail    = '操作失败';
    $op_lost    = '您要查找的资源可能已被删除或者暂时不可用';

    preg_match('/'.$op_success.'|'.$op_fail.'|'.$op_lost.'/is',$result,$matchs);

    $op_result = (isset($matchs[0])) ? $matchs[0] : '';

    if($op_result == $op_success){
        cli_out('operate received success');
        return true;
    }else if($op_result == $op_fail){
        cli_out('operate received fail');
        return false;
    }else if($op_result == $op_lost){
        cli_out('operate resource not found');
        return false;
    }else{
        cli_out('operate status unknown');
        return false;
    }
}

/**
 * 改变代理ip
 */
function change_proxy_ip(){

    global $cCurl;


    cli_out('start to get proxy_ip from yunip');

    $getnum = 1;
    //代理ip
    $ips        = get_ip_from_yunip($getnum);

    //$ips = array('122.71.25.205:8080');

    if(is_array($ips) && isset($ips[0])){
        $proxy_ip = rtrim($ips[0]);
    }else{
        //从云ip获取失败
        cli_out('yunip get fail');
        return false;
    }

    cli_out('yunip get proxy_ip:'.$proxy_ip);

    $cCurl->set_proxy($proxy_ip);


    cli_out('start to check proxy_ip:'.$proxy_ip.' , please wait...');

    //获取内容
    $fetch_result = $cCurl->get("http://kojiki.server.ne.jp/etc/pj235.cgi");

    cli_out('start to check proxy_ip from content');

    if(!isset($fetch_result['content'])){
        $cCurl->proxy_can_use = 0;
        //连接失败，无法获取代理IP验证内容
        cli_out('yunip get content empty');
        return false;
    }else{
        //网页内容
        $result = $fetch_result['content'];

        //添加入内容
        //$up_items = array('proxy_content'=> $result);
        //order_update($cCurl->order_id,$up_items);

        //是否能检查到原始ip
        preg_match('/HTTP_X_FORWARDED_FOR/is',$result,$for_matchs);
        if(!empty($for_matchs)){
            $cCurl->proxy_can_use = 0;
            //有
            //检测到原始IP,代理IP不可用
            cli_out("find original ip , can not use");
            return false;
        }


        //检查是否代理生效
        $url_part = explode(':',$proxy_ip);

        //端口
        $host_ip = $proxy_ip;
        if(isset($url_part[0])){
            $host_ip    = $url_part[0];
        }

        $reg_url = str_replace('.','\.',$host_ip);
        preg_match('/REMOTE_ADDR='.$reg_url.'/is',$result,$remote_matchs);

        if(empty($remote_matchs)){
            //没有
            $cCurl->proxy_can_use = 0;
            cli_out("proxy_content:".$result);
            cli_out("not find proxy ip , can not use");
            return false;
        }

        $cCurl->proxy_can_use = 1;
        cli_out("proxy_ip:$proxy_ip can use");

        return true;
    }
}

/*
确认订单
*/
function get_jd_confirm($order_id,$order_sn,$sid){

    global $cCurl;

    $referurl   = "http://m.jd.com/user/confirmSelect.action?orderId=$order_sn&sid=$sid";
    $get_url    = "http://m.jd.com/user/confirmGoods.action?orderId=$order_sn&sid=$sid";

    $response = $cCurl->get($get_url,$referurl);

    //记录sid内容
    //$up_items = array('confirm_content'=> $response['content']);
    //order_update($order_id,$up_items);

    return $response['content'];
}

/**
 * 获取订单详情
 * @param $order_id
 * @return array
 */
function get_order_info($order_id){

    global $db;

    $sql = <<<EOD
    SELECT
        o.order_id,
        o.order_sn,
        m.member_name,
        m.member_pwd,
        o.receive_status
    FROM orders o
    LEFT JOIN member m ON m.member_id = o.member_id
    WHERE o.order_id= $order_id
    AND o.member_id > 0
EOD;

    //读取需要抓取的列表
    $order = $db->get_one($sql);

    return $order;
}

/**
 * 修改订单状态
 * @param $order_id
 * @param $receive_status
 * @return int
 */
function order_receive_status_update($order_id,$receive_status){
    global $db;

    $time = date('Y-m-d H:i:s');

    $receive_sql = '';
    if($receive_status == RECEIVE_RECEIVEING){
        $receive_sql = ",receiveing_time = '$time'";
    }else if($receive_status == RECEIVE_RECEIVED){
        $receive_sql = ",received_time = '$time'";
    }

        $sql = <<<EOD
    UPDATE orders
    SET receive_status = $receive_status $receive_sql
    WHERE order_id= $order_id
EOD;

    //读取需要抓取的列表
    $db->query($sql);
    if($db->affected_rows()){
        $success = true;
    }else{
        $success = false;
    }

    return $success;
}



/**
 * 修改订单状态
 * @param $order_id
 * @param $receive_status
 * @return int
 */
function order_comment_status_update($order_id,$comment_status){
    global $db;

    $time = date('Y-m-d H:i:s');

    $comment_sql = '';
    if($comment_status == COMMENT_RECEIVEING){
        $comment_sql = ",commenting_time = '$time'";
    }else if($comment_status == COMMENT_RECEIVED){
        $comment_sql = ",commented_time = '$time'";
    }

    $sql = <<<EOD
    UPDATE orders
    SET comment_status = $comment_status $comment_sql
    WHERE order_id= $order_id
EOD;

    //读取需要抓取的列表
    $db->query($sql);
    if($db->affected_rows()){
        $success = true;
    }else{
        $success = false;
    }

    return $success;
}



/**
 * 从云中获取ip
 * @param int $getnum
 * @return array
 */
function get_ip_from_yunip($getnum = 1){

    //订单号
    $key = '608569532717240';

    $cYP = new YunIP();
    $result = $cYP->get_ip($key,$getnum);

    //换行分隔
    $ips = preg_split('/\\n/',$result);

    if(is_array($ips) && count($ips) > 0){
        //移出最后一个换行空元素
        array_pop($ips);
    }

    return $ips;
}


/**
 * 最离现在时间最早的提交的待确认的订单
 * @return array
 */
function get_receive_submit_order_count(){
    global $db;

    //已提交
    $receive_submit = RECEIVE_SUBMIT;

    $sql = <<<EOD
    SELECT
        COUNT(*) AS order_count
    FROM orders o
    LEFT JOIN member m ON m.member_id = o.member_id
    WHERE receive_status = $receive_submit
    ORDER BY receiveing_time ASC,order_id ASC
    LIMIT 0,1
EOD;

    //读取需要抓取的列表
    $order = $db->get_one($sql);

    if(empty($order)){
        return 0;
    }else{
        return $order['order_count'];
    }
}




/**
 * 最离现在时间最早的提交的待确认的订单
 * @return array
 */
function get_comment_submit_order_count(){
    global $db;

    //已提交
    $comment_submit = COMMENT_SUBMIT;
    //已收货
    $receive_received = RECEIVE_RECEIVED;

    $sql = <<<EOD
    SELECT
        COUNT(*) AS order_count
    FROM orders o
    LEFT JOIN member m ON m.member_id = o.member_id
    WHERE comment_status = $comment_submit
        AND receive_status = $receive_received
        AND comment != ''
    ORDER BY commenting_time ASC,order_id ASC
    LIMIT 0,1
EOD;

    //读取需要抓取的列表
    $order = $db->get_one($sql);

    if(empty($order)){
        return 0;
    }else{
        return $order['order_count'];
    }
}


/**
 * 最离现在时间最早的提交的待确认的订单
 * @return array
 */
function get_one_receive_submit_order(){
    global $db;

    //已提交
    $receive_submit = RECEIVE_SUBMIT;

    $sql = <<<EOD
    SELECT
        o.order_id,
        o.order_sn,
        m.member_name,
        m.member_pwd,
        m.member_pay_pwd,
        o.receive_status,
        t.user_id
    FROM orders o
    LEFT JOIN member m ON m.member_id = o.member_id
    LEFT JOIN task t ON t.task_id = o.task_id
    WHERE o.receive_status = $receive_submit
    ORDER BY o.receiveing_time ASC,o.order_id ASC
    LIMIT 0,1
EOD;

    //读取需要抓取的列表
    $order = $db->get_one($sql);

    return $order;
}


/**
 * 最离现在时间最早的提交的待确认的订单
 * @return array
 */
function get_last_received_order($where = array()){
    global $db;

    $whereSql = '';
    if(isset($where['user_id'])){
        $whereSql = ' AND t.user_id = '.$where['user_id'];
    }

    //已提交
    $receive_received = RECEIVE_RECEIVED;

    $sql = <<<EOD
    SELECT
        o.order_id,
        o.order_sn,
        t.user_id,
        o.task_id,
        o.received_time
    FROM orders o
    LEFT JOIN member m ON m.member_id = o.member_id
    LEFT JOIN task t ON t.task_id = o.task_id
    WHERE o.receive_status = $receive_received $whereSql
    ORDER BY o.received_time DESC
    LIMIT 0,1
EOD;

    //读取需要抓取的列表
    $order = $db->get_one($sql);

    return $order;
}


/**
 * 最离现在时间最早的提交的待确认的订单
 * @return array
 */
function get_last_received_order_goods($where = array()){
    global $db;

    $whereSql = '';
    if(isset($where['goods_id'])){
        $whereSql = ' AND og.goods_id = '.$where['goods_id'];
    }

    //已提交
    $receive_received = RECEIVE_RECEIVED;

    $sql = <<<EOD
    SELECT
        o.order_id,
        o.order_sn,
        t.user_id,
        o.task_id,
        o.received_time
    FROM orders o
    LEFT JOIN member m ON m.member_id = o.member_id
    LEFT JOIN task t ON t.task_id = o.task_id
    LEFT JOIN order_goods og ON og.order_id = o.order_id
    WHERE o.receive_status = $receive_received $whereSql
    ORDER BY o.received_time DESC
    LIMIT 0,1
EOD;

    //读取需要抓取的列表
    $order = $db->get_one($sql);

    return $order;
}



/**
 * 最离现在时间最早的提交的待确认的订单
 * @return array
 */
function get_last_commented_order_goods($where = array()){
    global $db;

    $whereSql = '';
    if(isset($where['goods_id'])){
        $whereSql = ' AND og.goods_id = '.$where['goods_id'];
    }

    //已提交
    $comment_received = COMMENT_RECEIVED;

    $sql = <<<EOD
    SELECT
        o.order_id,
        o.order_sn,
        t.user_id,
        o.task_id,
        o.received_time,
        o.comment_status,
        o.commenting_time,
        o.commented_time
    FROM orders o
    LEFT JOIN member m ON m.member_id = o.member_id
    LEFT JOIN task t ON t.task_id = o.task_id
    LEFT JOIN order_goods og ON og.order_id = o.order_id
    WHERE o.comment_status = $comment_received $whereSql
    ORDER BY o.commented_time DESC
    LIMIT 0,1
EOD;

    //读取需要抓取的列表
    $order = $db->get_one($sql);

    return $order;
}



/**
 * 修改订单的内容
 * @param $order_id
 * @param $up_items
 * @return int
 */
function order_update($order_id,$up_items){
    global $db;

    $upSql = '';
    foreach($up_items as $k => $v){
        $v = $db->escape_string($v);
        $upSql .= "{$k} = '{$v}',";
    }

    $upSql = rtrim($upSql,',');

    $sql = <<<EOD
    UPDATE orders SET $upSql WHERE order_id = $order_id
EOD;

    $db->query($sql);
    return $db->affected_rows();

}

/**
 * 是否在休息期
 * @return bool
 */
function at_rest_time_range(){

    $hour = (int)date('H');
    if($hour >=1 && $hour <=7){
        return true;
    }

    return false;
}



/**
 * 登陆京东用户，获取sid
 * @param $id
 * @param $username
 * @param $passwd
 * @return string
 */
function confirm_old_order($order_id,$member_name,$member_pwd,$member_pay_pwd){
    global $cCurl,$argv;

    $params = array(
        'order_id'          => $order_id,
        'username'          => $member_name,
        'password'          => $member_pwd,
        'payment_password'  => $member_pay_pwd
    );

    //推到哪个服务器
    $type = isset_set($argv[0],'test');

    if($type == 'test'){
        //测试
        $loginUrl ="http://b1.poptop.cc/api/v1/jd_receipt";
    }else if ($type == 'product'){
        //正式
        $loginUrl ="http://115.29.249.97:9292/api/v1/jd_receipt";
    }else{
        $loginUrl ="http://115.29.249.97:9292/api/v1/jd_receipt";
    }

    cli_out('loginUrl : '.$loginUrl);


    $fields = http_build_query($params);
    $timeout = 300;
    $response = $cCurl->post($loginUrl,$fields,$referurl="",$timeout);

    $curl_info = $response['curl_info'];

    //状态码
    $http_code = $curl_info['http_code'];
    //内容
    $content   = $response['content'];

    cli_out('http_code : '.$http_code);

    cli_out('content:'.$content);

    return $content;
}




/**
 * 评价一个订单
 * @param $id
 * @param $username
 * @param $passwd
 * @return string
 */
function comment_old_order($order_id,$product_id,$username,$password,$body){
    global $cCurl,$argv;

    $params = array(
        'order_id'          => $order_id,
        'product_id'        => $product_id,
        'username'          => $username,
        'password'          => $password,
        'body'              => $body
    );

    //推到哪个服务器
    $type = isset_set($argv[0],'test');

    if($type == 'test'){
        //测试
        $loginUrl ="http://b1.poptop.cc/api/v1/jd_comment";
    }else if ($type == 'product'){
        //正式
        $loginUrl ="http://115.29.249.97:9292/api/v1/jd_comment";
    }else{
        $loginUrl ="http://115.29.249.97:9292/api/v1/jd_comment";
    }

    cli_out('loginUrl : '.$loginUrl);


    $fields = http_build_query($params);
    $timeout = 300;
    $response = $cCurl->post($loginUrl,$fields,$referurl="",$timeout);

    $curl_info = $response['curl_info'];

    //状态码
    $http_code = $curl_info['http_code'];
    //内容
    $content   = $response['content'];

    cli_out('http_code : '.$http_code);

    cli_out('content:'.$content);

    return $content;
}





/**
 * 确认收货一个订单
 * @param $order
 */
function receive_old_jd_order($order){

    $order_sn       = $order['order_sn'];
    $member_name    = $order['member_name'];
    $member_pwd     = $order['member_pwd'];
    $member_pay_pwd = $order['member_pay_pwd'];

    cli_out('start get sid ,please wait...');

    $error_times = 0;
    $success = 0;
    do{

        //最多试5次
        if($error_times > 1){
            break;
        }else{
            cli_out("order :$order_sn receive try times ".($error_times + 1));
        }

        //获取收货确认
        $content = confirm_old_order($order_sn,$member_name,$member_pwd,$member_pay_pwd);

        if($content !== '18'){
            $error_times++;
        }else{
            $success = 1;
            break;
        }

    }while(true);


    if($success){
        //收货确认
        return true;

    }else{
        //收货失败
        return false;
    }
}



/**
 * 确认收货一个订单
 * @param $order
 */
function comment_old_jd_order($order){

    $order_id   = $order['order_id'];
    $order_sn   = $order['order_sn'];
    $product_id = $order['goods_id'];
    $username   = $order['member_name'];
    $password   = $order['member_pwd'];
    $body       = $order['comment'];

    cli_out('comment order_id:'.$order_id.' please wait...');

    $error_times = 0;
    $success = 0;
    do{

        //最多试5次
        if($error_times > 1){
            break;
        }else{
            cli_out("order :$order_sn receive try times ".($error_times + 1));
        }

        //获取收货确认
        $content = comment_old_order($order_sn,$product_id,$username,$password,$body);

        if($content !== 'true'){
            $error_times++;
        }else{
            $success = 1;
            break;
        }

    }while(true);


    if($success){
        //收货确认
        return true;

    }else{
        //收货失败
        return false;
    }
}



/**
 * 最离现在时间最早的提交的待确认的产品
 * @return array
 */
function get_one_receive_submit_goods(){
    global $db;

    //已提交
    $receive_submit = RECEIVE_SUBMIT;

    //正在收货的商品
    $goods_ids = get_one_receiving_goods();

    //去重复
    $goods_ids = implode(',',array_unique(explode(',',$goods_ids)));

    //最近收货成功的商品，排除，防止多等待
    $recent_received_goods = get_recent_received_goods();

    //排除正在收货的商品
    $notInSql = " AND og.goods_id NOT IN ($goods_ids,$recent_received_goods)";

    $sql = <<<EOD
    SELECT
        o.order_id,
        o.order_sn,
        m.member_name,
        m.member_pwd,
        m.member_pay_pwd,
        o.receive_status,
        t.user_id,
        t.task_id,
        og.goods_id
    FROM orders o
    LEFT JOIN member m ON m.member_id = o.member_id
    LEFT JOIN task t ON t.task_id = o.task_id
    LEFT JOIN order_goods og ON og.order_id = o.order_id
    WHERE o.receive_status = $receive_submit
    $notInSql
    ORDER BY o.receiveing_time ASC,o.order_id ASC
    LIMIT 0,1
EOD;

    //读取需要抓取的列表
    $order = $db->get_one($sql);

    return $order;
}



/**
 * 最离现在时间最早的提交的待确认的产品
 * @return array
 */
function get_one_comment_submit_goods(){
    global $db;

    //已提交
    $comment_submit = COMMENT_SUBMIT;

    //已收货确认
    $receive_received = RECEIVE_RECEIVED;

    //正在收货的商品
    $goods_ids = get_one_commenting_goods();

    //去重复
    $goods_ids = implode(',',array_unique(explode(',',$goods_ids)));

    //最近收货成功的商品，排除，防止多等待
    $recent_commented_goods = get_recent_commented_goods();

    //排除正在收货的商品
    $notInSql = " AND og.goods_id NOT IN ($goods_ids,$recent_commented_goods)";

    $sql = <<<EOD
    SELECT
        o.order_id,
        o.order_sn,
        m.member_name,
        m.member_pwd,
        m.member_pay_pwd,
        o.receive_status,
        t.user_id,
        t.task_id,
        og.goods_id,
        o.comment
    FROM orders o
    LEFT JOIN member m ON m.member_id = o.member_id
    LEFT JOIN task t ON t.task_id = o.task_id
    LEFT JOIN order_goods og ON og.order_id = o.order_id
    WHERE o.comment_status = $comment_submit
        AND o.receive_status = $receive_received
        AND o.comment != ''
    $notInSql
    ORDER BY o.commenting_time ASC,o.order_id ASC
    LIMIT 0,1
EOD;

    //读取需要抓取的列表
    $order = $db->get_one($sql);

    return $order;
}


/**
 * 正在收货中的商品id
 * @return array
 */
function get_one_receiving_goods(){
    global $db;

    //完毕
    $ts_finish = TS_FINISH;
    //收货中
    $receive_receiveing = RECEIVE_RECEIVEING;

    $sql = <<<EOD
    SELECT
        og.goods_id
    FROM orders o
    LEFT JOIN member m ON m.member_id = o.member_id
    LEFT JOIN task t ON t.task_id = o.task_id AND t.task_status = $ts_finish
    LEFT JOIN order_goods og ON og.order_id = o.order_id
    WHERE o.receive_status = $receive_receiveing
EOD;

    //读取需要抓取的列表
    $goods = $db->get_all($sql);

    //只要一列
    $goods_ids = get_array_cols($goods,'goods_id','string');

    return $goods_ids;
}



/**
 * 正在收货中的商品id
 * @return array
 */
function get_one_commenting_goods(){
    global $db;

    //完毕
    $ts_finish = TS_FINISH;

    //评价中
    $comment_receiveing = COMMENT_RECEIVEING;

    $sql = <<<EOD
    SELECT
        og.goods_id
    FROM orders o
    LEFT JOIN member m ON m.member_id = o.member_id
    LEFT JOIN task t ON t.task_id = o.task_id AND t.task_status = $ts_finish
    LEFT JOIN order_goods og ON og.order_id = o.order_id
    WHERE o.comment_status = $comment_receiveing
EOD;

    //读取需要抓取的列表
    $goods = $db->get_all($sql);

    //只要一列
    $goods_ids = get_array_cols($goods,'goods_id','string');

    return $goods_ids;
}


/**
 * 最近1分钟收货成功的商品
 * @return array
 */
function get_recent_received_goods(){
    global $db;

    //完毕
    $ts_finish = TS_FINISH;

    //收货成功
    $receive_received = RECEIVE_RECEIVED;


    //1分钟前的时间
    $before_time = (strtotime(full_date()) - 60);
    $before_time_fmt = full_date($before_time);

    $sql = <<<EOD
    SELECT
        DISTINCT(og.goods_id)
    FROM orders o
    LEFT JOIN member m ON m.member_id = o.member_id
    LEFT JOIN task t ON t.task_id = o.task_id AND t.task_status = $ts_finish
    LEFT JOIN order_goods og ON og.order_id = o.order_id
    WHERE o.receive_status = $receive_received
        AND o.received_time > '$before_time_fmt'
EOD;

    //读取需要抓取的列表
    $goods = $db->get_all($sql);

    //只要一列
    $goods_ids = get_array_cols($goods,'goods_id','string');

    return $goods_ids;
}


/**
 * 最近1分钟评价成功的商品
 * @return array
 */
function get_recent_commented_goods(){
    global $db;

    //完毕
    $ts_finish = TS_FINISH;

    //收货成功
    $comment_received = COMMENT_RECEIVED;


    //1分钟前的时间
    $before_time = (strtotime(full_date()) - 60);
    $before_time_fmt = full_date($before_time);

    $sql = <<<EOD
    SELECT
        DISTINCT(og.goods_id)
    FROM orders o
    LEFT JOIN member m ON m.member_id = o.member_id
    LEFT JOIN task t ON t.task_id = o.task_id AND t.task_status = $ts_finish
    LEFT JOIN order_goods og ON og.order_id = o.order_id
    WHERE o.comment_status = $comment_received
        AND o.commented_time > '$before_time_fmt'
EOD;

    //读取需要抓取的列表
    $goods = $db->get_all($sql);

    //只要一列
    $goods_ids = get_array_cols($goods,'goods_id','string');

    return $goods_ids;
}
?>